/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.CarParts;
import com.yuhanginfo.modules.sys.service.CarPartsService;

/**
 * 配件主数据Controller
 * @author yangs
 * @version 2017-10-10
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/carParts")
public class CarPartsController extends BaseController {

	@Autowired
	private CarPartsService carPartsService;
	
	@ModelAttribute
	public CarParts get(@RequestParam(required=false) String id) {
		CarParts entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = carPartsService.get(id);
		}
		if (entity == null){
			entity = new CarParts();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:carParts:view")
	@RequestMapping(value = {"list", ""})
	public String list(CarParts carParts, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CarParts> page = carPartsService.findPage(new Page<CarParts>(request, response), carParts); 
		model.addAttribute("page", page);
		return "modules/sys/carPartsList";
	}

	@RequiresPermissions("sys:carParts:view")
	@RequestMapping(value = "form")
	public String form(CarParts carParts, Model model) {
		model.addAttribute("carParts", carParts);
		return "modules/sys/carPartsForm";
	}

	@RequiresPermissions("sys:carParts:edit")
	@RequestMapping(value = "save")
	public String save(CarParts carParts, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, carParts)){
			return form(carParts, model);
		}
		carPartsService.save(carParts);
		addMessage(redirectAttributes, "保存配件主数据成功");
		return "redirect:"+Global.getAdminPath()+"/sys/carParts/?repage";
	}
	
	@RequiresPermissions("sys:carParts:edit")
	@RequestMapping(value = "delete")
	public String delete(CarParts carParts, RedirectAttributes redirectAttributes) {
		carPartsService.delete(carParts);
		addMessage(redirectAttributes, "删除配件主数据成功");
		return "redirect:"+Global.getAdminPath()+"/sys/carParts/?repage";
	}

}